home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / AlgoSampList.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  25.2 KB  |  846 lines  |  [TEXT/KAHL]

  1. /* AlgoSampList.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "AlgoSampList.h"
  31. #include "StringList.h"
  32. #include "Array.h"
  33. #include "Memory.h"
  34. #include "AlgoSampObject.h"
  35. #include "Alert.h"
  36. #include "DataMunging.h"
  37. #include "BufferedFileInput.h"
  38. #include "BufferedFileOutput.h"
  39. #include "Files.h"
  40. #include "Scrap.h"
  41.  
  42.  
  43. struct AlgoSampListRec
  44.     {
  45.         StringListRec*                    List;
  46.         struct CodeCenterRec*        CodeCenter;
  47.         struct MainWindowRec*        MainWindow;
  48.         ArrayRec*                                AlgoSampArray;
  49.         MyBoolean                                AlgoSampListChanged;
  50.     };
  51.  
  52.  
  53. #define MAGICSCRAPSTRING ("\xff\x00\x1f\xfe AlgoSampObjectScrap")
  54.  
  55.  
  56. /* create a new algorithmic sample list */
  57. AlgoSampListRec*        NewAlgoSampList(struct MainWindowRec* MainWindow,
  58.                                             struct CodeCenterRec* CodeCenter, WinType* ScreenID,
  59.                                             OrdType XLoc, OrdType YLoc, OrdType Width, OrdType Height)
  60.     {
  61.         AlgoSampListRec*    AlgoSampList;
  62.  
  63.         AlgoSampList = (AlgoSampListRec*)AllocPtrCanFail(sizeof(AlgoSampListRec),
  64.             "AlgoSampListRec");
  65.         if (AlgoSampList == NIL)
  66.             {
  67.              FailurePoint1:
  68.                 return NIL;
  69.             }
  70.         AlgoSampList->AlgoSampArray = NewArray();
  71.         if (AlgoSampList->AlgoSampArray == NIL)
  72.             {
  73.              FailurePoint2:
  74.                 ReleasePtr((char*)AlgoSampList);
  75.                 goto FailurePoint1;
  76.             }
  77.         AlgoSampList->List = NewStringList(ScreenID,XLoc,YLoc,Width,Height,
  78.             GetScreenFont(),9,StringListDontAllowMultipleSelection,"Algorithmic Samples");
  79.         if (AlgoSampList->List == NIL)
  80.             {
  81.              FailurePoint3:
  82.                 DisposeArray(AlgoSampList->AlgoSampArray);
  83.                 goto FailurePoint2;
  84.             }
  85.         AlgoSampList->CodeCenter = CodeCenter;
  86.         AlgoSampList->MainWindow = MainWindow;
  87.         AlgoSampList->AlgoSampListChanged = False;
  88.         return AlgoSampList;
  89.     }
  90.  
  91.  
  92. /* delete the algorithmic sample list and all of the samples it contains */
  93. void                                DisposeAlgoSampList(AlgoSampListRec* AlgoSampList)
  94.     {
  95.         long                            Scan;
  96.         long                            Limit;
  97.  
  98.         CheckPtrExistence(AlgoSampList);
  99.         Limit = ArrayGetLength(AlgoSampList->AlgoSampArray);
  100.         for (Scan = 0; Scan < Limit; Scan += 1)
  101.             {
  102.                 AlgoSampObjectRec*    AlgoSampTemp;
  103.  
  104.                 AlgoSampTemp = (AlgoSampObjectRec*)ArrayGetElement(
  105.                     AlgoSampList->AlgoSampArray,Scan);
  106.                 DisposeAlgoSampObject(AlgoSampTemp);
  107.             }
  108.         DisposeArray(AlgoSampList->AlgoSampArray);
  109.         DisposeStringList(AlgoSampList->List);
  110.         ReleasePtr((char*)AlgoSampList);
  111.     }
  112.  
  113.  
  114. /* change the location of the algorithmic sample list in the window */
  115. void                                SetAlgoSampListLocation(AlgoSampListRec* AlgoSampList,
  116.                                             OrdType XLoc, OrdType YLoc, OrdType Width, OrdType Height)
  117.     {
  118.         CheckPtrExistence(AlgoSampList);
  119.         SetStringListLoc(AlgoSampList->List,XLoc,YLoc,Width,Height);
  120.     }
  121.  
  122.  
  123. /* redraw the list */
  124. void                                AlgoSampListRedraw(AlgoSampListRec* AlgoSampList)
  125.     {
  126.         CheckPtrExistence(AlgoSampList);
  127.         RedrawStringList(AlgoSampList->List);
  128.     }
  129.  
  130.  
  131. /* see if the specified coordinates falls inside the sample list rectangle */
  132. MyBoolean                        AlgoSampListHitTest(AlgoSampListRec* AlgoSampList,
  133.                                             OrdType XLoc, OrdType YLoc)
  134.     {
  135.         CheckPtrExistence(AlgoSampList);
  136.         return StringListHitTest(AlgoSampList->List,XLoc,YLoc);
  137.     }
  138.  
  139.  
  140. /* handle a mouse down event for the algorithmic sample list */
  141. void                                AlgoSampListDoMouseDown(AlgoSampListRec* AlgoSampList,
  142.                                             OrdType XLoc, OrdType YLoc, ModifierFlags Modifiers)
  143.     {
  144.         CheckPtrExistence(AlgoSampList);
  145.         if (StringListMouseDown(AlgoSampList->List,XLoc,YLoc,Modifiers))
  146.             {
  147.                 /* if it returns true, then it was a double click */
  148.                 AlgoSampListOpenSelection(AlgoSampList);
  149.             }
  150.     }
  151.  
  152.  
  153. /* called when the window becomes active */
  154. void                                AlgoSampListBecomeActive(AlgoSampListRec* AlgoSampList)
  155.     {
  156.         CheckPtrExistence(AlgoSampList);
  157.         EnableStringList(AlgoSampList->List);
  158.     }
  159.  
  160.  
  161. /* called when the window becomes inactive */
  162. void                                AlgoSampListBecomeInactive(AlgoSampListRec* AlgoSampList)
  163.     {
  164.         CheckPtrExistence(AlgoSampList);
  165.         DisableStringList(AlgoSampList->List);
  166.     }
  167.  
  168.  
  169. /* called when a selection is made in another list, so that this list */
  170. /* is deselected */
  171. void                                AlgoSampListDeselect(AlgoSampListRec* AlgoSampList)
  172.     {
  173.         CheckPtrExistence(AlgoSampList);
  174.         DeselectAllStringListElements(AlgoSampList->List);
  175.     }
  176.  
  177.  
  178. /* check to see if there is a selection in this list */
  179. MyBoolean                        AlgoSampListIsThereSelection(AlgoSampListRec* AlgoSampList)
  180.     {
  181.         CheckPtrExistence(AlgoSampList);
  182.         return (GetStringListHowManySelectedItems(AlgoSampList->List) > 0);
  183.     }
  184.  
  185.  
  186. /* check to see if any of the algo samples contained in this list need */
  187. /* to be saved */
  188. MyBoolean                        DoesAlgoSampListNeedToBeSaved(AlgoSampListRec* AlgoSampList)
  189.     {
  190.         long                            Scan;
  191.         long                            Limit;
  192.         MyBoolean                    Flag;
  193.  
  194.         CheckPtrExistence(AlgoSampList);
  195.         Flag = AlgoSampList->AlgoSampListChanged;
  196.         Limit = ArrayGetLength(AlgoSampList->AlgoSampArray);
  197.         for (Scan = 0; (Scan < Limit) && !Flag; Scan += 1)
  198.             {
  199.                 AlgoSampObjectRec*    AlgoSampTemp;
  200.  
  201.                 AlgoSampTemp = (AlgoSampObjectRec*)ArrayGetElement(
  202.                     AlgoSampList->AlgoSampArray,Scan);
  203.                 if (HasAlgoSampObjectBeenModified(AlgoSampTemp))
  204.                     {
  205.                         Flag = True;
  206.                     }
  207.             }
  208.         return Flag;
  209.     }
  210.  
  211.  
  212. /* open an edit window for the selected algorithmic sample */
  213. void                                AlgoSampListOpenSelection(AlgoSampListRec* AlgoSampList)
  214.     {
  215.         ArrayRec*                    ListOfSelections;
  216.  
  217.         CheckPtrExistence(AlgoSampList);
  218.         ListOfSelections = GetListOfSelectedItems(AlgoSampList->List);
  219.         if (ListOfSelections != NIL)
  220.             {
  221.                 long                            Scan;
  222.                 long                            Limit;
  223.  
  224.                 Limit = ArrayGetLength(ListOfSelections);
  225.                 for (Scan = 0; Scan < Limit; Scan += 1)
  226.                     {
  227.                         AlgoSampObjectRec*    AlgoSampTemp;
  228.  
  229.                         AlgoSampTemp = (AlgoSampObjectRec*)ArrayGetElement(ListOfSelections,Scan);
  230.                         AlgoSampObjectOpenWindow(AlgoSampTemp);
  231.                     }
  232.                 DisposeArray(ListOfSelections);
  233.             }
  234.     }
  235.  
  236.  
  237. /* create a new algorithmic sample and open a window for it */
  238. void                                AlgoSampListNewAlgoSamp(AlgoSampListRec* AlgoSampList)
  239.     {
  240.         AlgoSampObjectRec*    AlgoSamp;
  241.  
  242.         CheckPtrExistence(AlgoSampList);
  243.         /* create the object */
  244.         AlgoSamp = NewAlgoSampObject(AlgoSampList->CodeCenter,AlgoSampList->MainWindow,
  245.             AlgoSampList);
  246.         if (AlgoSamp == NIL)
  247.             {
  248.              FailurePoint1:
  249.                 AlertHalt("There is not enough memory available to create a new "
  250.                     "algorithmic sample.",NIL);
  251.                 return;
  252.             }
  253.         /* add it to the string list */
  254.         if (!InsertStringListElement(AlgoSampList->List,NIL,NIL,AlgoSamp,True))
  255.             {
  256.              FailurePoint2:
  257.                 DisposeAlgoSampObject(AlgoSamp);
  258.                 goto FailurePoint1;
  259.             }
  260.         MainWindowDeselectAllOtherStringLists(AlgoSampList->MainWindow,AlgoSampList);
  261.         SelectStringListElement(AlgoSampList->List,AlgoSamp);
  262.         MakeStringListSelectionVisible(AlgoSampList->List);
  263.         /* add it to the array */
  264.         if (!ArrayAppendElement(AlgoSampList->AlgoSampArray,AlgoSamp))
  265.             {
  266.              FailurePoint3:
  267.                 RemoveStringListElement(AlgoSampList->List,AlgoSamp,True);
  268.                 goto FailurePoint2;
  269.             }
  270.         /* update our internal flags */
  271.         AlgoSampList->AlgoSampListChanged = True;
  272.         /* change the name in the list */
  273.         AlgoSampListAlgoSampNameChanged(AlgoSampList,AlgoSamp);
  274.         /* show the window */
  275.         AlgoSampObjectOpenWindow(AlgoSamp);
  276.     }
  277.  
  278.  
  279. /* delete the selected algorithmic sample */
  280. void                                AlgoSampListDeleteSelection(AlgoSampListRec* AlgoSampList)
  281.     {
  282.         ArrayRec*                    ListOfSelections;
  283.  
  284.         CheckPtrExistence(AlgoSampList);
  285.         ListOfSelections = GetListOfSelectedItems(AlgoSampList->List);
  286.         if (ListOfSelections != NIL)
  287.             {
  288.                 long                                Scan;
  289.                 long                                Limit;
  290.  
  291.                 Limit = ArrayGetLength(ListOfSelections);
  292.                 for (Scan = 0; Scan < Limit; Scan += 1)
  293.                     {
  294.                         AlgoSampObjectRec*    OneToZap;
  295.  
  296.                         OneToZap = (AlgoSampObjectRec*)ArrayGetElement(ListOfSelections,Scan);
  297.                         AlgoSampListDeleteAlgoSamp(AlgoSampList,OneToZap);
  298.                     }
  299.                 DisposeArray(ListOfSelections);
  300.             }
  301.     }
  302.  
  303.  
  304. /* delete the explicitly specified algorithmic sample */
  305. void                                AlgoSampListDeleteAlgoSamp(AlgoSampListRec* AlgoSampList,
  306.                                             struct AlgoSampObjectRec* TheAlgoSamp)
  307.     {
  308.         long                                Scan;
  309.         long                                Limit;
  310.  
  311.         CheckPtrExistence(AlgoSampList);
  312.         Limit = ArrayGetLength(AlgoSampList->AlgoSampArray);
  313.         for (Scan = 0; Scan < Limit; Scan += 1)
  314.             {
  315.                 AlgoSampObjectRec*    AlgoSampTemp;
  316.  
  317.                 AlgoSampTemp = (AlgoSampObjectRec*)ArrayGetElement(
  318.                     AlgoSampList->AlgoSampArray,Scan);
  319.                 if (TheAlgoSamp == AlgoSampTemp)
  320.                     {
  321.                         FileSpec*                    BackupFileWhere;
  322.                         FileType*                    BackupFile;
  323.                         MyBoolean                    Success = False;
  324.  
  325.                         BackupFileWhere = NewTempFileSpec(CODE4BYTES('?','?','?','?'),
  326.                             CODE4BYTES('?','?','?','?'));
  327.                         if (BackupFileWhere != NIL)
  328.                             {
  329.                                 if (OpenFile(BackupFileWhere,&BackupFile,eReadAndWrite))
  330.                                     {
  331.                                         BufferedOutputRec*    Output;
  332.  
  333.                                         Output = NewBufferedOutput(BackupFile);
  334.                                         if (Output != NIL)
  335.                                             {
  336.                                                 if (WriteBufferedOutput(Output,sizeof(MAGICSCRAPSTRING),
  337.                                                     MAGICSCRAPSTRING))
  338.                                                     {
  339.                                                         if (AlgoSampObjectWriteOutData(TheAlgoSamp,Output)
  340.                                                             == eFileLoadNoError)
  341.                                                             {
  342.                                                                 Success = True;
  343.                                                             }
  344.                                                     }
  345.                                                 if (!EndBufferedOutput(Output))
  346.                                                     {
  347.                                                         Success = False;
  348.                                                     }
  349.                                             }
  350.                                          else
  351.                                             {
  352.                                                 CloseFile(BackupFile);
  353.                                             }
  354.                                     }
  355.                                  else
  356.                                     {
  357.                                         DeleteFile(BackupFileWhere);
  358.                                         DisposeFileSpec(BackupFileWhere);
  359.                                     }
  360.                             }
  361.                         if (Success)
  362.                             {
  363.                                 MainWindowNewDeleteUndoInfo(AlgoSampList->MainWindow,BackupFileWhere,
  364.                                     BackupFile);
  365.                                 DisposeAlgoSampObject(AlgoSampTemp);
  366.                                 RemoveStringListElement(AlgoSampList->List,AlgoSampTemp,True);
  367.                                 ArrayDeleteElement(AlgoSampList->AlgoSampArray,Scan);
  368.                                 AlgoSampList->AlgoSampListChanged = True;
  369.                             }
  370.                          else
  371.                             {
  372.                                 YesNoCancelType        Decision;
  373.  
  374.                                 Decision = AskYesNoCancel("Unable to save undo information for object.  "
  375.                                     "Delete object anyway?",NIL,"Delete","Cancel",NIL/*nothirdbutton*/);
  376.                                 if (Decision == eYes)
  377.                                     {
  378.                                         DisposeAlgoSampObject(AlgoSampTemp);
  379.                                         RemoveStringListElement(AlgoSampList->List,AlgoSampTemp,True);
  380.                                         ArrayDeleteElement(AlgoSampList->AlgoSampArray,Scan);
  381.                                         AlgoSampList->AlgoSampListChanged = True;
  382.                                     }
  383.                             }
  384.                         return;
  385.                     }
  386.             }
  387.         EXECUTE(PRERR(AllowResume,"AlgoSampListDeleteAlgoSamp:  couldn't find object"));
  388.     }
  389.  
  390.  
  391. /* the name of a algorithmic sample has changed, so the name in the scrolling */
  392. /* list must also be changed */
  393. void                                AlgoSampListAlgoSampNameChanged(AlgoSampListRec* AlgoSampList,
  394.                                             struct AlgoSampObjectRec* TheAlgoSamp)
  395.     {
  396.         char*                            AlgoSampName;
  397.  
  398.         CheckPtrExistence(AlgoSampList);
  399.         CheckPtrExistence(TheAlgoSamp);
  400.         ERROR(ArrayFindElement(AlgoSampList->AlgoSampArray,TheAlgoSamp) < 0,
  401.             PRERR(ForceAbort,"AlgoSampListAlgoSampNameChanged:  unknown algosamp"));
  402.         AlgoSampName = AlgoSampObjectGetNameCopy(TheAlgoSamp);
  403.         if (AlgoSampName != NIL)
  404.             {
  405.                 char*                            AlgoSampNameNullTerminated;
  406.  
  407.                 AlgoSampNameNullTerminated = BlockToStringCopy(AlgoSampName);
  408.                 if (AlgoSampNameNullTerminated != NIL)
  409.                     {
  410.                         ChangeStringListElementName(AlgoSampList->List,
  411.                             AlgoSampNameNullTerminated,TheAlgoSamp);
  412.                         ReleasePtr(AlgoSampNameNullTerminated);
  413.                     }
  414.                 ReleasePtr(AlgoSampName);
  415.             }
  416.     }
  417.  
  418.  
  419. /* look for a specified algorithmic sample.  returns NIL if not found.  the name is */
  420. /* NOT null terminated */
  421. AlgoSampObjectRec*    AlgoSampListLookupNamedAlgoSamp(
  422.                                             AlgoSampListRec* AlgoSampList, char* Name)
  423.     {
  424.         long                            Scan;
  425.         long                            Limit;
  426.  
  427.         CheckPtrExistence(AlgoSampList);
  428.         CheckPtrExistence(Name);
  429.         Limit = ArrayGetLength(AlgoSampList->AlgoSampArray);
  430.         for (Scan = 0; Scan < Limit; Scan += 1)
  431.             {
  432.                 AlgoSampObjectRec*    AlgoSamp;
  433.                 char*                                NameCopy;
  434.  
  435.                 AlgoSamp = (AlgoSampObjectRec*)ArrayGetElement(AlgoSampList->AlgoSampArray,Scan);
  436.                 NameCopy = AlgoSampObjectGetNameCopy(AlgoSamp);
  437.                 if (NameCopy != NIL)
  438.                     {
  439.                         if (PtrSize(Name) == PtrSize(NameCopy))
  440.                             {
  441.                                 if (MemEqu(Name,NameCopy,PtrSize(Name)))
  442.                                     {
  443.                                         ReleasePtr(NameCopy);
  444.                                         return AlgoSamp;
  445.                                     }
  446.                             }
  447.                         ReleasePtr(NameCopy);
  448.                     }
  449.             }
  450.         return NIL;
  451.     }
  452.  
  453.  
  454. /* remove all data arrays for all algorithmic samples */
  455. void                                AlgoSampListUnbuildAll(AlgoSampListRec* AlgoSampList)
  456.     {
  457.         long                            Scan;
  458.         long                            Limit;
  459.  
  460.         CheckPtrExistence(AlgoSampList);
  461.         Limit = ArrayGetLength(AlgoSampList->AlgoSampArray);
  462.         for (Scan = 0; Scan < Limit; Scan += 1)
  463.             {
  464.                 AlgoSampObjectRec*    AlgoSampTemp;
  465.  
  466.                 AlgoSampTemp = (AlgoSampObjectRec*)ArrayGetElement(
  467.                     AlgoSampList->AlgoSampArray,Scan);
  468.                 AlgoSampObjectUnbuild(AlgoSampTemp);
  469.             }
  470.     }
  471.  
  472.  
  473. /* build all algorithmic sample tables.  returns True if successful. */
  474. MyBoolean                        AlgoSampListMakeUpToDate(AlgoSampListRec* AlgoSampList)
  475.     {
  476.         long                            Scan;
  477.         long                            Limit;
  478.  
  479.         CheckPtrExistence(AlgoSampList);
  480.         Limit = ArrayGetLength(AlgoSampList->AlgoSampArray);
  481.         for (Scan = 0; Scan < Limit; Scan += 1)
  482.             {
  483.                 AlgoSampObjectRec*    AlgoSampTemp;
  484.  
  485.                 AlgoSampTemp = (AlgoSampObjectRec*)ArrayGetElement(
  486.                     AlgoSampList->AlgoSampArray,Scan);
  487.                 if (!AlgoSampObjectMakeUpToDate(AlgoSampTemp))
  488.                     {
  489.                         return False;
  490.                     }
  491.             }
  492.         return True;
  493.     }
  494.  
  495.  
  496. /* document's name changed, so we have to update the windows */
  497. void                                AlgoSampListGlobalNameChange(AlgoSampListRec* AlgoSampList,
  498.                                             char* NewFilename)
  499.     {
  500.         long                            Scan;
  501.         long                            Limit;
  502.  
  503.         CheckPtrExistence(AlgoSampList);
  504.         Limit = ArrayGetLength(AlgoSampList->AlgoSampArray);
  505.         for (Scan = 0; Scan < Limit; Scan += 1)
  506.             {
  507.                 AlgoSampObjectRec*    AlgoSampTemp;
  508.  
  509.                 AlgoSampTemp = (AlgoSampObjectRec*)ArrayGetElement(
  510.                     AlgoSampList->AlgoSampArray,Scan);
  511.                 AlgoSampObjectGlobalNameChange(AlgoSampTemp,NewFilename);
  512.             }
  513.     }
  514.  
  515.  
  516. /*   4-byte little endian number of algorithmic sample objects (positive 2s complement) */
  517. /*   n-byte data for the algorithmic sample objects */
  518.  
  519.  
  520. /* read algorithmic sample objects from a file.  returns True if completely successful. */
  521. FileLoadingErrors        AlgoSampListReadData(AlgoSampListRec* AlgoSampList,
  522.                                             struct BufferedInputRec* Input)
  523.     {
  524.         signed long                NumSampleObjects;
  525.         long                            Scan;
  526.  
  527.         CheckPtrExistence(AlgoSampList);
  528.         CheckPtrExistence(Input);
  529.  
  530.         /*   4-byte little endian number of objects (positive 2s complement) */
  531.         if (!ReadBufferedSignedLongLittleEndian(Input,&NumSampleObjects))
  532.             {
  533.                 return eFileLoadDiskError;
  534.             }
  535.         if (NumSampleObjects < 0)
  536.             {
  537.                 return eFileLoadBadFormat;
  538.             }
  539.  
  540.         /* load things from the file */
  541.         for (Scan = 0; Scan < NumSampleObjects; Scan += 1)
  542.             {
  543.                 AlgoSampObjectRec*    AlgoSampTemp EXECUTE(= (AlgoSampObjectRec*)0x81818181);
  544.                 FileLoadingErrors        Error;
  545.  
  546.                 /* read in the object */
  547.                 Error = AlgoSampObjectNewFromFile(&AlgoSampTemp,Input,AlgoSampList->CodeCenter,
  548.                     AlgoSampList->MainWindow,AlgoSampList);
  549.                 if (Error != eFileLoadNoError)
  550.                     {
  551.                      FailurePoint1:
  552.                         return Error;
  553.                     }
  554.                 CheckPtrExistence(AlgoSampTemp);
  555.                 /* add it to the string list */
  556.                 if (!InsertStringListElement(AlgoSampList->List,NIL,NIL,AlgoSampTemp,True))
  557.                     {
  558.                      FailurePoint2:
  559.                         DisposeAlgoSampObject(AlgoSampTemp);
  560.                         Error = eFileLoadOutOfMemory;
  561.                         goto FailurePoint1;
  562.                     }
  563.                 /* add it to the array */
  564.                 if (!ArrayAppendElement(AlgoSampList->AlgoSampArray,AlgoSampTemp))
  565.                     {
  566.                      FailurePoint3:
  567.                         RemoveStringListElement(AlgoSampList->List,AlgoSampTemp,True);
  568.                         goto FailurePoint2;
  569.                     }
  570.                 /* change the name in the list */
  571.                 AlgoSampListAlgoSampNameChanged(AlgoSampList,AlgoSampTemp);
  572.             }
  573.  
  574.         return eFileLoadNoError;
  575.     }
  576.  
  577.  
  578. /* write algorithmic sample objects to a file.  returns True if completely successful. */
  579. FileLoadingErrors        AlgoSampListWriteData(AlgoSampListRec* AlgoSampList,
  580.                                             struct BufferedOutputRec* Output)
  581.     {
  582.         long                            NumberOfObjects;
  583.         long                            Scan;
  584.  
  585.         CheckPtrExistence(AlgoSampList);
  586.         CheckPtrExistence(Output);
  587.  
  588.         /*   4-byte little endian number of objects (positive 2s complement) */
  589.         NumberOfObjects = ArrayGetLength(AlgoSampList->AlgoSampArray);
  590.         if (!WriteBufferedSignedLongLittleEndian(Output,NumberOfObjects))
  591.             {
  592.                 return eFileLoadDiskError;
  593.             }
  594.  
  595.         /* write out the objects */
  596.         for (Scan = 0; Scan < NumberOfObjects; Scan += 1)
  597.             {
  598.                 AlgoSampObjectRec*    AlgoSampTemp;
  599.                 FileLoadingErrors        Error;
  600.  
  601.                 /* get the object */
  602.                 AlgoSampTemp = (AlgoSampObjectRec*)ArrayGetElement(
  603.                     AlgoSampList->AlgoSampArray,Scan);
  604.                 /* write it out */
  605.                 Error = AlgoSampObjectWriteOutData(AlgoSampTemp,Output);
  606.                 /* handle any errors */
  607.                 if (Error != eFileLoadNoError)
  608.                     {
  609.                         return Error;
  610.                     }
  611.             }
  612.  
  613.         return eFileLoadNoError;
  614.     }
  615.  
  616.  
  617. /* after a file has been saved, this is called to mark all objects as not modified. */
  618. void                                AlgoSampListMarkAllObjectsSaved(AlgoSampListRec* AlgoSampList)
  619.     {
  620.         long                            Scan;
  621.         long                            Limit;
  622.  
  623.         CheckPtrExistence(AlgoSampList);
  624.         Limit = ArrayGetLength(AlgoSampList->AlgoSampArray);
  625.         for (Scan = 0; Scan < Limit; Scan += 1)
  626.             {
  627.                 AlgoSampObjectRec*    AlgoSampTemp;
  628.  
  629.                 AlgoSampTemp = (AlgoSampObjectRec*)ArrayGetElement(
  630.                     AlgoSampList->AlgoSampArray,Scan);
  631.                 AlgoSampObjectMarkAsSaved(AlgoSampTemp);
  632.             }
  633.         AlgoSampList->AlgoSampListChanged = False;
  634.     }
  635.  
  636.  
  637. /* copy the selected object in the list to the clipboard.  return False if failed. */
  638. MyBoolean                        AlgoSampListCopyObject(AlgoSampListRec* AlgoSampList)
  639.     {
  640.         ArrayRec*                            ListOfSelections;
  641.         MyBoolean                            TotalSuccessFlag = False;
  642.  
  643.         CheckPtrExistence(AlgoSampList);
  644.         ListOfSelections = GetListOfSelectedItems(AlgoSampList->List);
  645.         if (ListOfSelections != NIL)
  646.             {
  647.                 if (ArrayGetLength(ListOfSelections) >= 1)
  648.                     {
  649.                         AlgoSampObjectRec*    AlgoSampTemp;
  650.                         FileSpec*                        TempFileLocation;
  651.  
  652.                         AlgoSampTemp = (AlgoSampObjectRec*)ArrayGetElement(ListOfSelections,0);
  653.                         /* open the temporary file */
  654.                         TempFileLocation = NewTempFileSpec(CODE4BYTES('\?','\?','\?','\?'),
  655.                             CODE4BYTES('\?','\?','\?','\?'));
  656.                         if (TempFileLocation != NIL)
  657.                             {
  658.                                 FileType*                            FileDescriptor;
  659.  
  660.                                 if (OpenFile(TempFileLocation,&FileDescriptor,eReadAndWrite))
  661.                                     {
  662.                                         BufferedOutputRec*        BufferedFile;
  663.  
  664.                                         BufferedFile = NewBufferedOutput(FileDescriptor);
  665.                                         if (BufferedFile != NIL)
  666.                                             {
  667.                                                 MyBoolean                            WriteSucceeded = False;
  668.  
  669.                                                 if (WriteBufferedOutput(BufferedFile,sizeof(MAGICSCRAPSTRING),
  670.                                                     MAGICSCRAPSTRING))
  671.                                                     {
  672.                                                         if (AlgoSampObjectWriteOutData(AlgoSampTemp,BufferedFile)
  673.                                                             == eFileLoadNoError)
  674.                                                             {
  675.                                                                 WriteSucceeded = True;
  676.                                                             }
  677.                                                     }
  678.                                                 if (EndBufferedOutput(BufferedFile) && WriteSucceeded)
  679.                                                     {
  680.                                                         char*                            Buffer;
  681.                                                         long                            NumberOfBytes;
  682.  
  683.                                                         NumberOfBytes = GetFileLength(FileDescriptor);
  684.                                                         Buffer = AllocPtrCanFail(NumberOfBytes,
  685.                                                             "AlgoSampListCopyObject:  scrap buffer");
  686.                                                         if (Buffer != NIL)
  687.                                                             {
  688.                                                                 if (SetFilePosition(FileDescriptor,0))
  689.                                                                     {
  690.                                                                         if (0 == ReadFromFile(FileDescriptor,
  691.                                                                             Buffer,NumberOfBytes))
  692.                                                                             {
  693.                                                                                 if (SetScrapToThis(Buffer))
  694.                                                                                     {
  695.                                                                                         TotalSuccessFlag = True;
  696.                                                                                     }
  697.                                                                             }
  698.                                                                     }
  699.                                                                 ReleasePtr(Buffer);
  700.                                                             }
  701.                                                     }
  702.                                             }
  703.                                         CloseFile(FileDescriptor);
  704.                                     }
  705.                                 DeleteFile(TempFileLocation);
  706.                                 DisposeFileSpec(TempFileLocation);
  707.                             }
  708.                     }
  709.                 DisposeArray(ListOfSelections);
  710.             }
  711.         return TotalSuccessFlag;
  712.     }
  713.  
  714.  
  715. /* try to paste the clipboard in as an algorithmic sample object.  returns False if */
  716. /* it failed or the clipboard did not contain an algorithmic sample object. */
  717. MyBoolean                        AlgoSampListPasteObject(AlgoSampListRec* AlgoSampList)
  718.     {
  719.         MyBoolean                    TotalSuccessFlag = False;
  720.         char*                            Scrap;
  721.  
  722.         CheckPtrExistence(AlgoSampList);
  723.         Scrap = GetCopyOfScrap();
  724.         if (Scrap != NIL)
  725.             {
  726.                 FileSpec*                    TempFileLocation;
  727.  
  728.                 TempFileLocation = NewTempFileSpec(CODE4BYTES('\?','\?','\?','\?'),
  729.                     CODE4BYTES('\?','\?','\?','\?'));
  730.                 if (TempFileLocation != NIL)
  731.                     {
  732.                         FileType*                            FileDescriptor;
  733.  
  734.                         if (OpenFile(TempFileLocation,&FileDescriptor,eReadAndWrite))
  735.                             {
  736.                                 BufferedOutputRec*        BufferedFile;
  737.  
  738.                                 BufferedFile = NewBufferedOutput(FileDescriptor);
  739.                                 if (BufferedFile != NIL)
  740.                                     {
  741.                                         MyBoolean                            WriteSucceeded = False;
  742.  
  743.                                         if (WriteBufferedOutput(BufferedFile,PtrSize(Scrap),Scrap))
  744.                                             {
  745.                                                 WriteSucceeded = True;
  746.                                             }
  747.                                         if (EndBufferedOutput(BufferedFile) && WriteSucceeded)
  748.                                             {
  749.                                                 TotalSuccessFlag = AlgoSampListPasteFromFile(
  750.                                                     AlgoSampList,FileDescriptor);
  751.                                             }
  752.                                     }
  753.                                 CloseFile(FileDescriptor);
  754.                             }
  755.                         DeleteFile(TempFileLocation);
  756.                         DisposeFileSpec(TempFileLocation);
  757.                     }
  758.                 ReleasePtr(Scrap);
  759.             }
  760.         return TotalSuccessFlag;
  761.     }
  762.  
  763.  
  764. /* try to paste an algorithmic sample object from the file */
  765. MyBoolean                        AlgoSampListPasteFromFile(AlgoSampListRec* AlgoSampList,
  766.                                             struct FileType* File)
  767.     {
  768.         MyBoolean                    TotalSuccessFlag = False;
  769.  
  770.         CheckPtrExistence(AlgoSampList);
  771.         if (SetFilePosition(File,0))
  772.             {
  773.                 BufferedInputRec*    InputFile;
  774.  
  775.                 InputFile = NewBufferedInput(File);
  776.                 if (InputFile != NIL)
  777.                     {
  778.                         char                            HeaderTest[sizeof(MAGICSCRAPSTRING)];
  779.  
  780.                         if (ReadBufferedInput(InputFile,sizeof(MAGICSCRAPSTRING),HeaderTest))
  781.                             {
  782.                                 if (MemEqu(MAGICSCRAPSTRING,HeaderTest,sizeof(MAGICSCRAPSTRING)))
  783.                                     {
  784.                                         AlgoSampObjectRec*        AlgoSampTemp EXECUTE(= (AlgoSampObjectRec*)0x81818181);
  785.  
  786.                                         if (eFileLoadNoError == AlgoSampObjectNewFromFile(&AlgoSampTemp,InputFile,
  787.                                             AlgoSampList->CodeCenter,AlgoSampList->MainWindow,AlgoSampList))
  788.                                             {
  789.                                                 CheckPtrExistence(AlgoSampTemp);
  790.                                                 /* add it to the scrolling object list */
  791.                                                 if (!InsertStringListElement(AlgoSampList->List,NIL,NIL,AlgoSampTemp,True))
  792.                                                     {
  793.                                                      FailurePoint:
  794.                                                         DisposeAlgoSampObject(AlgoSampTemp);
  795.                                                     }
  796.                                                  else
  797.                                                     {
  798.                                                         MainWindowDeselectAllOtherStringLists(AlgoSampList->MainWindow,AlgoSampList);
  799.                                                         SelectStringListElement(AlgoSampList->List,AlgoSampTemp);
  800.                                                         MakeStringListSelectionVisible(AlgoSampList->List);
  801.                                                         /* add it to the array */
  802.                                                         if (!ArrayAppendElement(AlgoSampList->AlgoSampArray,AlgoSampTemp))
  803.                                                             {
  804.                                                                 RemoveStringListElement(AlgoSampList->List,AlgoSampTemp,True);
  805.                                                                 goto FailurePoint;
  806.                                                             }
  807.                                                          else
  808.                                                             {
  809.                                                                 /* change the name in the list */
  810.                                                                 AlgoSampListAlgoSampNameChanged(AlgoSampList,AlgoSampTemp);
  811.                                                                 TotalSuccessFlag = True;
  812.                                                                 AlgoSampList->AlgoSampListChanged = True;
  813.                                                             }
  814.                                                     }
  815.                                             }
  816.                                     }
  817.                             }
  818.                         EndBufferedInput(InputFile);
  819.                     }
  820.             }
  821.         return TotalSuccessFlag;
  822.     }
  823.  
  824.  
  825. /* find out how many algorithmic samples there are in this list */
  826. long                                AlgoSampListHowMany(AlgoSampListRec* AlgoSampList)
  827.     {
  828.         CheckPtrExistence(AlgoSampList);
  829.         return ArrayGetLength(AlgoSampList->AlgoSampArray);
  830.     }
  831.  
  832.  
  833. /* get an indexed algorithmic sample from the list */
  834. struct AlgoSampObjectRec*    AlgoSampListGetIndexedAlgoSamp(AlgoSampListRec* AlgoSampList,
  835.                                             long Index)
  836.     {
  837.         AlgoSampObjectRec*    AlgoSamp;
  838.  
  839.         CheckPtrExistence(AlgoSampList);
  840.         ERROR((Index < 0) || (Index >= AlgoSampListHowMany(AlgoSampList)),PRERR(ForceAbort,
  841.             "AlgoSampListGetIndexedAlgoSamp:  index out of range"));
  842.         AlgoSamp = (AlgoSampObjectRec*)ArrayGetElement(AlgoSampList->AlgoSampArray,Index);
  843.         CheckPtrExistence(AlgoSamp);
  844.         return AlgoSamp;
  845.     }
  846.